using System;
using System.ServiceProcess;
using SuiHan.Commons.Logging;
using SuiHan.Commons.Util;

namespace SuiHan.ProcessDaemon.WinService {
    /// <summary>
    /// 守护进程Windows服务
    /// </summary>
    public class DaemonService : ServiceBase {
        private readonly RunOptions options;

        public DaemonService(RunOptions options) {
            this.options = options;
        }

        protected override void OnStart(string[] args) {
            if (!Enum.TryParse(options.LogLevel.ToUpper(), out LoggerLevel level)) {
                level = Env.IsProd ? LoggerLevel.ERROR : LoggerLevel.DEBUG;
            }

            LoggerFactory.Config(new LoggerConfig { Level = level, ConsoleEnabled = Env.IsDev || options.ConsoleEnabled, FileEnabled = Env.IsProd });
            DaemonApplication.Start();
            ThreadUtils.HoldOn();
        }

        protected override void OnStop() {
            DaemonApplication.Shutdown();
        }
    }
}
